﻿using Power.Models;
using Power.View.Report;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Power.ViewModel.Report
{
    /// <summary>
    /// 检查井检查情况汇总表
    /// </summary>
    public class CheckTableViewModel : ReportTableBase
    {
        public List<InspectTableData> Data { get; set; }
        private CheckTable model;
        public CheckTableViewModel(CheckTable model)
        {
            this.model = model;
            Data = new List<InspectTableData>()
            {
                CreateEntity(2),
                CreateEntity(5),
            };

            Init();
        }

        /// <summary>
        /// 动态创建测试数据
        /// </summary>
        /// <param name="subCount"></param>
        /// <returns></returns>
        public InspectTableData CreateEntity(int subCount)
        {
            var obj = Activator.CreateInstance(typeof(InspectTableData));

            var type = obj.GetType();
            var props = type.GetProperties();
            foreach (var item in props)
            {
                if (item.PropertyType == typeof(int))
                {
                    item.SetValue(obj, 1);
                }
                else if (item.PropertyType == typeof(string))
                {
                    item.SetValue(obj, "2");
                }
            }

            var ret = obj as InspectTableData;
            ret.BaseValue = new List<InspectTableBase>();
            for (int i = 0; i < subCount; i++)
            {
                ret.BaseValue.Add(CreateSubEntity());
            }
            return ret;
        }
        /// <summary>
        /// 动态创建子项测试数据
        /// </summary>
        /// <returns></returns>
        public InspectTableBase CreateSubEntity()
        {
            var obj = Activator.CreateInstance(typeof(InspectTableBase));

            var type = obj.GetType();
            var props = type.GetProperties();
            foreach (var item in props)
            {
                if (item.PropertyType == typeof(int))
                {
                    item.SetValue(obj, 1);
                }
                else if (item.PropertyType == typeof(string))
                {
                    item.SetValue(obj, "2");
                }
            }

            return obj as InspectTableBase;
        }
        public void Init()
        {
            var color1 = new SolidColorBrush(Color.FromRgb(0, 0, 255));
            var color2 = new SolidColorBrush(Color.FromRgb(255, 255, 0));
            var color3 = new SolidColorBrush(Color.FromRgb(255, 102, 0));
            var color4 = new SolidColorBrush(Color.FromRgb(255, 0, 0));

            Row = Data.SelectMany(p => p.BaseValue).Count() + 2;
            Column = 13;

            var table = this.model.table;

            for (int i = 0; i < Row; i++)
            {
                table.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) });

            }
            for (int i = 0; i < Column; i++)
            {
                table.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(180) });
            }

            //画标题
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < Column; j++)
                {
                    DrawColumnSpanCell(table, i, j, "检查井检查情况汇总表", Column, 0, 0);

                    DrawCell(table, i, j, "序号", 1, 0);
                    DrawCell(table, i, j, "检查井类型", 1, 1);
                    DrawCell(table, i, j, "材质", 1, 2);
                    DrawCell(table, i, j, "单位", 1, 3);
                    DrawCell(table, i, j, "数量", 1, 4);
                    DrawCell(table, i, j, "其中非道路下数量", 1, 5);
                    DrawCell(table, i, j, "完好数量", 1, 6);
                    DrawCell(table, i, j, "井盖井座缺失数量", 1, 7);
                    DrawCell(table, i, j, "井内有杂物数量", 1, 8);
                    DrawCell(table, i, j, "井内有缺陷数量", 1, 9);
                    DrawCell(table, i, j, "盖框突出或缺损数量", 1, 10);
                    DrawCell(table, i, j, "井室周围填土有沉降数量", 1, 11);
                    DrawCell(table, i, j, "备注", 1, 12);
                }
            }

            //画内容
            var firstRow = 2;
            var currentRow = 2;
            for (int i = 2; i < Data.Count+2; i++)
            {
                var item = Data[i-2];
                for (int j = 0; j < item.BaseValue.Count; j++)
                {
                    var row = item.BaseValue[j];

                    var columnIndex = 0;

                    DrawRowSpanCell(table, currentRow, columnIndex, item.Order.ToString(), item.BaseValue.Count, firstRow, columnIndex++);//33
                    DrawRowSpanCell(table, currentRow, columnIndex, item.Type, item.BaseValue.Count, firstRow, columnIndex++);//33


                    DrawCell(table, currentRow, columnIndex, row.Mat, currentRow, columnIndex++);
                    DrawCell(table, currentRow, columnIndex, row.Unit, currentRow, columnIndex++);
                    DrawCell(table, currentRow, columnIndex, row.Number.ToString(), currentRow, columnIndex++);
                    DrawCell(table, currentRow, columnIndex, row.NumberNoUnderRoad.ToString(), currentRow, columnIndex++);
                    DrawCell(table, currentRow, columnIndex, row.NumberPerfect.ToString(), currentRow, columnIndex++);
                    DrawCell(table, currentRow, columnIndex, row.NumberMissing.ToString(), currentRow, columnIndex++);
                    DrawCell(table, currentRow, columnIndex, row.NumberWithThings.ToString(), currentRow, columnIndex++);
                    DrawCell(table, currentRow, columnIndex, row.NumberWithDefent.ToString(), currentRow, columnIndex++);
                    DrawCell(table, currentRow, columnIndex, row.NumberHatDefent.ToString(), currentRow, columnIndex++);
                    DrawCell(table, currentRow, columnIndex, row.NumberWithSick.ToString(), currentRow, columnIndex++); 
                    DrawCell(table, currentRow, columnIndex, row.Remark, currentRow, columnIndex++);

                    currentRow++;
                }
                firstRow = currentRow;
            }

            //补边框
            DrawBorder(table);
        }
    }
}
